package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class Register1 extends AppCompatActivity {

    private EditText mPhone;
    private EditText mName;
    private EditText mPassword;
    private Button mRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register1);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void initData() {
        String url = "http://anli-wechat.boruiman.com/foodserver/user/regist";
        String phoneStr = mPhone.getText().toString().trim();
        String nameStr = mName.getText().toString().trim();
        String passwordStr = mPassword.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone",phoneStr);
            jsonObject.put("password",passwordStr);
            jsonObject.put("name",nameStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(Register1.this);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(Register1.this, "cg", Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(Register1.this, "sb", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
        mPhone = findViewById(R.id.et_phone1);
        mName = findViewById(R.id.et_name);
        mPassword = findViewById(R.id.et_password2);
        mRegister = findViewById(R.id.btn_register);
    }
}